SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[SP_TMDOCU_PROV_Q50]                        
/*-------------------------------------------------------------------*/                                          
/*----- Empresa           : OFISIS S.A.                          ----*/                                          
/*----- Cliente           : OFISIS S.A.                          ----*/                                          
/*----- Sistema           : TESORERIA                            ----*/                                          
/*----- Modulo            : Empresa                              ----*/                                          
/*----- Programa          : Reporte REG COMPRAS                  ----*/                                          
/*----- Nombre            : SP_TMDOCU_PROV_Q50                   ----*/                                          
/*----- Script            : TMDOCU_PROV                          ----*/                                          
/*----- Desarrollado por  : Percy Garc­a                         ----*/                                          
/*----- Fecha             : 30/11/2005                           ----*/                                          
/*----- Base de Datos     : MS SQL Server                        ----*/                                          
/*----- Version BD        : 2000                                 ----*/                                          
/*-------------------------------------------------------------------*/                                                          
/*-------------------------------------------------------------------*/                                          
/*----- Drop proc NM_TMDOCU_PROV_Q50                             ----*/                                          
/*----- Grant ALL on NM_TMDOCU_PROV_Q50 to PUBLIC            ----*/                                          
/*-----                    
 NM_TMDOCU_PROV_Q50 '01','SOL','DOL','P','3','M',                    
'   And FE_REGI_COMP >=  convert(datetime,"01/02/2005",103) And FE_REGI_COMP <=  convert(datetime ,"28/02/2005",103)   '                                                       
   ----*/                                          
/*-------------------------------------------------------------------*/                                          
                                   
@ISCO_EMPR                TD_VC_002,                                          
@ISCO_USUA_MODI           TD_VC_008_USUA,                                          
@ISCO_MONE_NACI           TD_VC_003,                    
@ISCO_MONE_EXTR           TD_VC_003,                    
@ISCO_TIAU_PROV           TD_VC_001,                    
--@ISOR_SELE                Varchar(100),                    
@ISTI_REPO                TD_VC_001,                    
@ISTI_REPO_0002           TD_VC_001, --- 'O' OFICIAL 'M' MONEDA ORIGEN                    
@ISCA_WHER_LOCA           Varchar(8000)                    
                    
--- @ISTI_REPO                    
--'1' TIPO DOC SUNAT/FECHA EMISION                    
--'2' TIPO DOC SUNAT/PROVEEDOR/NUMERO DOCUM                    
--'3' TIPO DOC SUNAT/CORRELATIVO                    
--'4' PROVEEDOR                    
--'5' SOLO CORRELATIVO                    
                              
As                                          
                                                             
Declare                                  
@VSOR_SELE               Varchar(100),                    
@VSCA_ORDE_0001          Varchar(50) ,                
            
@VSCO_MONE_NACI  TD_VC_003,                          
@VSCO_MONE_EXTR  TD_VC_003,                          
@VSTI_CAMB   TD_VC_001,                          
@VSTI_ORIG   TD_VC_001,                          
@VSCO_TIAU_PROV  TD_VC_001,                          
@VNIM_MAXI_NRET  TD_NU_016_004,                          
@VNNU_REGI_ELIM  TD_IN_001              
                    
Create Table #TWDOCU_PROV_FTEERAL                     
(              
DE_TIPO_REPO   Varchar(150) NULL,                     
CO_TIPO         Varchar(50) NULL,                    
FE_EMIS      DateTime NULL,                    
NU_CORR_COMP    Varchar(50) NULL,                    
CO_SUNA         Varchar(50) NULL,                    
NU_DOCU_PROV    Varchar(50) NULL,                    
CO_RUCC         Varchar(50) NULL,                    
CO_RUCC_0001    Varchar(50) NULL,                    
NO_CORT_PROV    Varchar(100) NULL,                    
NO_CORT_PROV_0001 Varchar(100) NULL,                    
IM_BASE_GRA1    Numeric(16,4) NULL,                    
IM_BASE_GRA2    Numeric(16,4) NULL,                    
IM_BAIM_SCFI    Numeric(16,4) NULL,               
IM_INAF_NACI    Numeric(16,4) NULL,                    
IM_IMSC_BAIM    Numeric(16,4) NULL,                    
IM_IMP1_DOG1    Numeric(16,4) NULL,                    
IM_IMP1_DOG2    Numeric(16,4) NULL,                    
IM_IMP1_SCFI    Numeric(16,4) NULL,                    
IM_IMPU_RENT    Numeric(16,4) NULL,                    
IM_IMPU_SOLI    Numeric(16,4) NULL,                    
IM_TOTA_NACI    Numeric(16,4) NULL,                    
ST_SIGN         Varchar(1) NULL,                    
CO_ESTA_DOCU    Varchar(3) NULL,                    
NU_COMP_ORDE    Varchar(50) NULL,---20                    
CO_SUNA_ORIG    Varchar(2) NULL,                    
NU_DOCU_ORIG    Varchar(50) NULL,                     
NO_DOCU         Varchar(50) NULL,                     
NU_ASTO_REPO    Varchar(20) NULL,                    
FE_DEPO_DETR    DateTime NULL,                     
ST_RETE         Varchar(1) NULL,                     
FA_CAMB_REPO    Numeric(15,6) NULL,                     
CO_MONE_0001    Varchar(3) NULL,                    
VA_ORDE_0001    Int NULL,                    
ST_FLAG_VISU    Varchar(1) NULL,                    
VA_ORDE_GENE    Int NULL,                    
ST_FLAG_VIFI    Varchar(1) default 'N',                    
ST_FLFI_REPO    Varchar(1) default 'N',                    
                    
IM_BASE_GRA1_PAR1 Numeric(16,4) NULL,                    
IM_BASE_GRA2_PAR1 Numeric(16,4) NULL,                    
IM_BAIM_SCFI_PAR1 Numeric(16,4) NULL,                    
IM_INAF_NACI_PAR1 Numeric(16,4) NULL,                    
IM_IMSC_BAIM_PAR1 Numeric(16,4) NULL,                    
IM_IMP1_DOG1_PAR1 Numeric(16,4) NULL,                    
IM_IMP1_DOG2_PAR1 Numeric(16,4) NULL,                    
IM_IMP1_SCFI_PAR1 Numeric(16,4) NULL,                    
IM_IMPU_RENT_PAR1 Numeric(16,4) NULL,                    
IM_IMPU_SOLI_PAR1 Numeric(16,4) NULL,                    
IM_TOTA_NACI_PAR1 Numeric(16,4) NULL,                    
                    
IM_BASE_GRA1_FIN1 Numeric(16,4) NULL,                    
IM_BASE_GRA2_FIN1 Numeric(16,4) NULL,                    
IM_BAIM_SCFI_FIN1 Numeric(16,4) NULL,                    
IM_INAF_NACI_FIN1 Numeric(16,4) NULL,                    
IM_IMSC_BAIM_FIN1 Numeric(16,4) NULL,                    
IM_IMP1_DOG1_FIN1 Numeric(16,4) NULL,                    
IM_IMP1_DOG2_FIN1 Numeric(16,4) NULL,                    
IM_IMP1_SCFI_FIN1 Numeric(16,4) NULL,                    
IM_IMPU_RENT_FIN1 Numeric(16,4) NULL,                    
IM_IMPU_SOLI_FIN1 Numeric(16,4) NULL,                    
IM_TOTA_NACI_FIN1 Numeric(16,4) NULL,                    
DE_FINA_0001      Varchar(100) NULL,                    
DE_FINN_REPO      Varchar(100) NULL ,            
FE_ULTI_PAGO      DateTime NULL,            
CO_TIPO_OPER   Varchar(3) NULL,            
NU_ASTO Varchar(8) NULL,            
NU_RUCS  Varchar(20) NULL,            
CO_TIPO_DOCU Varchar(3) NULL,            
CO_MONE Varchar(3) NULL,            
FA_TIPO_CAMB Numeric(15,6) NULL,            
FA_CAMB_EXTR  Numeric(15,6)  Null,            
 FA_CAMB  Numeric(15,6)  Null,            
CO_IMP1   VarChar(03)  Null,            
 PC_IMP1   Numeric(9,4)  Null,            
 IM_IMP1   Numeric(16,4)  Null,            
 CO_IMP2   VarChar(03)  Null,            
 PC_IMP2   Numeric(9,4)  Null,            
 IM_IMP2   Numeric(16,4)  Null,            
 CO_IMP3   VarChar(03)  Null,            
 PC_IMP3   Numeric(9,4)  Null,            
 FE_DOCU_ORIG  DateTime  Null,            
 NU_DEPO_DETR  VarChar(20)  Null,            
 NU_COMP_SERV  VarChar(20)  Null,            
 FL_RETE   VarChar(1)  Null,            
IM_AJUS   Numeric(16,4)  Null   ,      
NU_FORM_NDCL  Varchar(20) NULL      
)                    
                    
            
                    
If @ISCA_WHER_LOCA IS NULL                     
Begin                     
 Select @ISCA_WHER_LOCA = ' '                    
End                    
                         
                          
Select @VSTI_CAMB = TI_CAMB, @VSTI_ORIG = TI_ORIG            From TTFACA_OPCI                                
Where CO_EMPR = @ISCO_EMPR                                
And CO_MODU = 'TEEM0001'                                
And CO_MENU = 'FTEEMBA'                                
                                
If ( @VSTI_CAMB Is Null Or @VSTI_ORIG Is Null )                          
 Select @VSTI_CAMB = TI_CAMB, @VSTI_ORIG = TI_ORIG,                          
   @VSCO_MONE_NACI = CO_MONE_DEFA, @VSCO_MONE_EXTR = CO_MONE_EXTR,                          
   @VSCO_TIAU_PROV = CO_TIAU_PROV,                          
   @VNIM_MAXI_NRET = IM_MAXI_NRET                          
 From TMPARA_TESO                                
 Where CO_EMPR = @ISCO_EMPR                                
Else                                
 Select @VSCO_MONE_NACI = CO_MONE_DEFA,                           
   @VSCO_MONE_EXTR = CO_MONE_EXTR,                          
   @VSCO_TIAU_PROV = CO_TIAU_PROV,                          
   @VNIM_MAXI_NRET = IM_MAXI_NRET                          
 From TMPARA_TESO                                
 Where CO_EMPR = @ISCO_EMPR                          
                          
If @ISTI_REPO_0002 = 'O'                    
Begin                    
            
Insert Into #TWDOCU_PROV_FTEERAL                     
(CO_TIPO, CO_SUNA,   FE_EMIS,   NU_CORR_COMP,  CO_TIPO_OPER, NU_ASTO_REPO,   NU_DOCU_PROV,  CO_RUCC,NO_CORT_PROV,                         
  CO_TIPO_DOCU, CO_MONE,            
  IM_BASE_GRA1,                    
  IM_BASE_GRA2,IM_BAIM_SCFI,IM_INAF_NACI,IM_IMSC_BAIM,IM_IMP1_DOG1,IM_IMP1_DOG2,IM_IMP1_SCFI,                    
  IM_IMPU_RENT,IM_IMPU_SOLI,IM_TOTA_NACI,   FA_TIPO_CAMB, FA_CAMB_EXTR, CO_IMP1,   PC_IMP1,     CO_IMP2,                         
  PC_IMP2,    CO_IMP3,   PC_IMP3,    ST_SIGN,   CO_ESTA_DOCU, CO_SUNA_ORIG, NU_DOCU_ORIG,   FE_DOCU_ORIG,  NU_DEPO_DETR,                         
  FE_DEPO_DETR, NU_COMP_SERV,  FL_RETE,   FE_ULTI_PAGO, NO_DOCU, VA_ORDE_0001,ST_FLAG_VISU, NU_FORM_NDCL)                          
 Select                             
  'REG_COMPRAS' As TIPO, T3.CO_SUNA As CODIGO_SUNAT, TMDOCU_PROV.FE_EMIS As FECHA_EMISION,                          
  TMDOCU_PROV.NU_CORR_COMP As CORRELATIVO, TMDOCU_PROV.CO_TIPO_OPER As CO_TIPO_OPER, TMDOCU_PROV.NU_ASTO As NRO_ASIENTO,      
  TMDOCU_PROV.NU_DOCU_PROV As NUMERO_DOCUMENTO, T2.NU_RUCS As RUC_PROVEEDOR,     
Isnull(NULLIF(T2.NO_RAZO_SOCI, ''), NULLIF(RTRIM(Isnull(T2.AP_PATE_AUXI, '') + ' ' + Isnull(T2.AP_MATE_AUXI, '') + ' ' +  Isnull(T2.NO_NATU_AUX1, '') + ' ' +  Isnull(T2.NO_NATU_AUX2, '')), '')),     
--T2.NO_AUXI As NOMB_PROV,    
  TMDOCU_PROV.CO_TIPO_DOCU, TMDOCU_PROV.CO_MONE As MONE,                          
  ---B1.---                          
  Case                           
   When T3.ST_DOCU_0027 = 'S' Then                          
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
    TMDOCU_PROV.IM_BRUT_AFEC, @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)                  
   When T3.ST_DOCU_0026 = 'S' Then 0.00                          
   When T3.ST_DOCU_0023 = 'S' Or T3.ST_DOCU_0024 = 'S' Then 0.00                           
   Else                           
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                        
    TMDOCU_PROV.IM_BRUT_AFEC -                           
    IsNull(TMDOCU_PROV.IM_SCRF,0) +                          
    PatIndex(TMDOCU_PROV.ST_AFEC_GAFI, 'S') * TMDOCU_PROV.IM_GAFI -                           
    (TMDOCU_PROV.IM_DCT1 + TMDOCU_PROV.IM_DCT2),                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)                           
  End As IB_BRUT_AFEC,                          
  ---B2.---                          
  Case                           
   When T3.ST_DOCU_0026 = 'S' Then 0.00           
   When T3.ST_DOCU_0027 = 'S' Then 0.00                          
   When T3.ST_DOCU_0023 = 'S' Or T3.ST_DOCU_0024 = 'S' Then 0.00                          
   Else Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
    IsNull(TMDOCU_PROV.IM_SCRF,0),                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)                          
  End As IB_OPER_GRAV,                          
  ---B3.---                          
  Case                           
   When T3.ST_DOCU_0026 = 'S' Then           
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
    IsNull(TMDOCU_PROV.IM_BRUT_AFEC,0),                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)                           
   When T3.ST_DOCU_0027 = 'S' Then 0.00                          
   When T3.ST_DOCU_0023 = 'S' Or T3.ST_DOCU_0024 = 'S' Then                          
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
    IsNull(TMDOCU_PROV.IM_BRUT_AFEC,0),                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)                          
   Else 0.00                          
  End As IB_OPER_SCRF,                          
  ---B4.---                          
  Case                     
--   When T3.ST_DOCU_0026 = 'S' Then 0.00                  
   When T3.ST_DOCU_0028 = 'S' Then                 
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
    IsNull(TMDOCU_PROV.IM_BRUT_INAF,0),                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)                   
   When T3.ST_DOCU_0027 = 'S' Then 0.00                                     
   When T3.ST_DOCU_0023 = 'S' Or T3.ST_DOCU_0024 = 'S' Then 0.00                          
   Else                           
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
    TMDOCU_PROV.IM_BRUT_INAF +                           
    PatIndex(TMDOCU_PROV.ST_AFEC_GAFI, 'N') * TMDOCU_PROV.IM_GAFI +                           
    PatIndex(TMDOCU_PROV.ST_AFEC_FLET, 'N') * TMDOCU_PROV.IM_FLET +                           
    PatIndex(TMDOCU_PROV.ST_AFEC_MORA, 'N') * TMDOCU_PROV.IM_MORA,                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)                          
  End As IB_OPER_INAF,                          
  ---B5.---                          
     0.00 As IB_SELE_CONS,                          
  ---I.1---                        
  Case                           
   When T3.ST_DOCU_0027 = 'S' Then                          
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
    IsNull(TMDOCU_PROV.IM_IMP1,0),                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)                          
   When T3.ST_DOCU_0026 = 'S' Then 0.00                          
  When T3.ST_DOCU_0023 = 'S' Or T3.ST_DOCU_0024 = 'S' Then 0.00                           
   Else     ISNULL((                      
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
    (( TMDOCU_PROV.IM_IMP1 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP1, '0'), 1, 2), 'IG' ) ) +                          
    (  TMDOCU_PROV.IM_IMP2 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP2, '0'), 1, 2), 'IG' ) ) +                          
    (  TMDOCU_PROV.IM_IMP3 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP3, '0'), 1, 2), 'IG' ) ) ),                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2) ),0)                          
  End As IG_OPER_GRAV,                          
  ---I.2---                           
  Case                           
   When T3.ST_DOCU_0026 = 'S' Then 0.00                          
   When T3.ST_DOCU_0027 = 'S' Then 0.00                          
   When T3.ST_DOCU_0023 = 'S' Or T3.ST_DOCU_0024 = 'S' Then 0.00                           
   Else    ISNULL((                      
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,               
                    
    ( ( TMDOCU_PROV.IM_SCF1 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP1, '0'), 1, 2), 'IG' ) ) +                          
     ( TMDOCU_PROV.IM_SCF2 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP2, '0'), 1, 2), 'IG' ) ) +                          
     ( TMDOCU_PROV.IM_SCF3 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP3, '0'), 1, 2), 'IG' ) ) ),                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)),0)                        
  End As IG_OPSE_GRAV,                          
  ---I.3---                           
  Case                                   
   When T3.ST_DOCU_0026 = 'S' Then             
   ABS(Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,          
   (SELECT TDDISP_CCOS.IM_DIST           
  FROM   TDDISP_CCOS           
  WHERE  TDDISP_CCOS.CO_EMPR = TMDOCU_PROV.CO_EMPR          
 AND    TDDISP_CCOS.CO_PROV = TMDOCU_PROV.CO_PROV          
  AND    TDDISP_CCOS.CO_TIPO_DOCU = TMDOCU_PROV.CO_TIPO_DOCU          
  AND    TDDISP_CCOS.NU_DOCU_PROV = TMDOCU_PROV.NU_DOCU_PROV          
  AND    TDDISP_CCOS.CO_CNTA_EMPR = '4010102'),           
 @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR , TMDOCU_PROV.FA_TIPO_CAMB), 2))                   
   Else 0.00                           
  End IG_OPSI_CRFI,                          
                           
  0.00 As IR_CUAR_CATE,                          
                           
    Case                           
   When T3.ST_DOCU_0026 = 'S' Then                          
    Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
    ( TMDOCU_PROV.IM_IMP1 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP1, '0'), 1, 2), 'ND' ) ),                           
    @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)                                 
   Else    0.00                         
  End  As IM_IESS,                          
                           
  Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
  TMDOCU_PROV.IM_TOTA,                           
  @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2) As IM_TOTA,                          
                           
  TMDOCU_PROV.FA_TIPO_CAMB, TMDOCU_PROV.FA_CAMB_EXTR,                           
  TMDOCU_PROV.CO_IMP1, TMDOCU_PROV.PC_IMP1,                          
  TMDOCU_PROV.CO_IMP2, TMDOCU_PROV.PC_IMP2,                          
  TMDOCU_PROV.CO_IMP3, TMDOCU_PROV.PC_IMP3,                          
    
  T3.ST_SIGN As SIGNO,                          
                           
  TMDOCU_PROV.CO_ESTA_DOCU As ESTADO,                          
  T5.CO_SUNA, TMDOCU_PROV.NU_DOCU_ORIG, TMDOCU_PROV.FE_DOCU_ORIG,                           
  TMDOCU_PROV.NU_DEPO_DETR As COMPROBANTE, TMDOCU_PROV.FE_DEPO_DETR As FE_DEPO_DETRACCION,                           
  TMDOCU_PROV.NU_COMP_SERV,                          
  DBO.FU_RETE_FLAG( TMDOCU_PROV.CO_EMPR, T2.ST_RETE, T2.ST_BCON, T3.ST_RETE,  TMDOCU_PROV.FE_EMIS,  @VNIM_MAXI_NRET, TMDOCU_PROV.IM_IMP1, T3.ST_DOCU_0004,                          
  dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI,TMDOCU_PROV.FE_EMIS, TMDOCU_PROV.IM_TOTA, @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR,                          
  TMDOCU_PROV.FA_TIPO_CAMB))  'RETENCION' ,                       
 (CASE                          
  When  TMDOCU_PROV.CO_TIPO_DOCU IN (Select TI_DOCU_PROV From #TWTIPO_DOCU) THEN                          
  DBO.FU_FECH_PAGO( @ISCO_EMPR, TMDOCU_PROV.CO_PROV,  TMDOCU_PROV.CO_TIPO_DOCU, TMDOCU_PROV.NU_DOCU_PROV, TMDOCU_PROV.FE_VENC )                          
  Else                          
  NULL                          
   END) 'FE_VENC', T3.NO_DOCU, 2, 'N',       
   Case                           
   When T3.ST_DOCU_0027 = 'S' Then                          
    ''                                             
   Else    ''
  End  As NU_FORM_NDCL                          
      
 From  TMDOCU_PROV                        
 Left Outer Join TTDOCU_CNTB T5                         
 On TMDOCU_PROV.TI_DOCU_ORIG = T5.TI_DOCU, TMAUXI_EMPR T2, TTDOCU_CNTB T3                          
 Where  TMDOCU_PROV.CO_EMPR  = @ISCO_EMPR                          
     And T2.CO_EMPR = TMDOCU_PROV.CO_EMPR                          
     And T2.TI_AUXI_EMPR = @VSCO_TIAU_PROV                          
     And T2.              
CO_AUXI_EMPR = TMDOCU_PROV.CO_PROV                          
     And T3.TI_DOCU = TMDOCU_PROV.CO_TIPO_DOCU                          
     And T3.ST_DOCU_0010 = 'N'                          
     And T3.ST_REGI_COMP = 'S'                          
 -- And T5.TI_DOCU =* TMDOCU_PROV.TI_DOCU_ORIG                          
     And Exists ( Select #TWREGI_COMP_0002.NU_CORR_COMP                          
      From #TWREGI_COMP_0002                          
      Where                           
      #TWREGI_COMP_0002.CO_PROV = TMDOCU_PROV.CO_PROV And                          
      #TWREGI_COMP_0002.CO_TIPO_DOCU = TMDOCU_PROV.CO_TIPO_DOCU And                          
  #TWREGI_COMP_0002.NU_DOCU_PROV = TMDOCU_PROV.NU_DOCU_PROV                          
     )                          
 Union All                          
 Select                             
  'REG_RECxHON' As TIPO, T3.CO_SUNA As CODIGO_SUNAT, TMDOCU_PROV.FE_EMIS As FECHA_EMISION,                          
  TMDOCU_PROV.NU_CORR_COMP As CORRELATIVO, TMDOCU_PROV.CO_TIPO_OPER As CO_TIPO_OPER, TMDOCU_PROV.NU_ASTO As NRO_ASIENTO,  TMDOCU_PROV.NU_DOCU_PROV As NUMERO_DOCUMENTO,                          
  T2.NU_RUCS As RUC_PROVEEDOR,     
Isnull(NULLIF(T2.NO_RAZO_SOCI, ''), NULLIF(RTRIM(Isnull(T2.AP_PATE_AUXI, '') + ' ' + Isnull(T2.AP_MATE_AUXI, '') + ' ' +  Isnull(T2.NO_NATU_AUX1, '') + ' ' +  Isnull(T2.NO_NATU_AUX2, '')), ''))As NOMB_PROV,    
--T2.NO_AUXI As NOMB_PROV,                          
  TMDOCU_PROV.CO_TIPO_DOCU, TMDOCU_PROV.CO_MONE As MONE,                          
  ---B1.---                          
  0.00 As IB_BRUT_AFEC,                          
  ---B2.---                          
  0.00 As IB_OPER_GRAV,                          
  ---B3.---                          
  0.00 As IB_OPER_SCRF,                          
 /* ---B4.---                          
  0.00 As IB_OPER_INAF,   */            
            
  Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, 'SOL', TMDOCU_PROV.FE_EMIS,                    
   TMDOCU_PROV.IM_BRUT_INAF + TMDOCU_PROV.IM_GAFI - TMDOCU_PROV.IM_DCT1 - TMDOCU_PROV.IM_DCT2,                    
   'C', 'O', 'S', 'SOL', 'DOL', TMDOCU_PROV.FA_TIPO_CAMB), 2) As IB_SELE_CON2,                           
    ---B5.---                    
  Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                    
   TMDOCU_PROV.IM_BRUT_AFEC + TMDOCU_PROV.IM_GAFI - TMDOCU_PROV.IM_DCT1 - TMDOCU_PROV.IM_DCT2,                    
   @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2) As IB_SELE_CON1,              
                
  ---I.1---                    
  0.00 As IG_OPER_GRAV,               
                  
  ---I.2---                           
  0.00 As IG_OPSE_GRAV,                          
  ---I.3---       
  0.00 As IG_OPSI_CRFI,                          
                           
  Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                      
  (( TMDOCU_PROV.IM_IMP1 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP1, '0'), 1, 2), 'IR' ) ) +                          
  (  TMDOCU_PROV.IM_IMP2 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP2, '0'), 1, 2), 'IR' ) ) +                          
  (  TMDOCU_PROV.IM_IMP3 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP3, '0'), 1, 2), 'IR' ) ) ),                           
  @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2) As IR_CUAR_CATE,                          
                           
  Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
  (( TMDOCU_PROV.IM_IMP1 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP1, '0'), 1, 3), 'IES' ) ) +                          
  (  TMDOCU_PROV.IM_IMP2 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP2, '0'), 1, 3), 'IES' ) ) +                          
  (  TMDOCU_PROV.IM_IMP3 * PATINDEX( SUBSTRING( IsNull( TMDOCU_PROV.CO_IMP3, '0'), 1, 3), 'IES' ) ) ),                           
  @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2)  As IM_IESS,                          
                           
  Round(OFITESO.dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI, TMDOCU_PROV.FE_EMIS,                          
  TMDOCU_PROV.IM_TOTA,                           
  @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, TMDOCU_PROV.FA_TIPO_CAMB), 2) As IM_TOTA,                          
                           
  TMDOCU_PROV.FA_TIPO_CAMB, TMDOCU_PROV.FA_CAMB_EXTR,                           
  TMDOCU_PROV.CO_IMP1, TMDOCU_PROV.PC_IMP1,                          
  TMDOCU_PROV.CO_IMP2, TMDOCU_PROV.PC_IMP2,                          
  TMDOCU_PROV.CO_IMP3, TMDOCU_PROV.PC_IMP3,                          
                           
  T3.ST_SIGN As SIGNO,                          
                           
  TMDOCU_PROV.CO_ESTA_DOCU As ESTADO,                          
  T5.CO_SUNA, TMDOCU_PROV.NU_DOCU_ORIG, TMDOCU_PROV.FE_DOCU_ORIG,                           
  TMDOCU_PROV.NU_DEPO_DETR As COMPROBANTE, TMDOCU_PROV.FE_DEPO_DETR As FE_DEPO_DETRACCION,                           
  TMDOCU_PROV.NU_COMP_SERV,                
                        
  DBO.FU_RETE_FLAG( TMDOCU_PROV.CO_EMPR, T2.ST_RETE, T2.ST_BCON, T3.ST_RETE,  TMDOCU_PROV.FE_EMIS,  @VNIM_MAXI_NRET, TMDOCU_PROV.IM_IMP1, T3.ST_DOCU_0004,                          
  dbo.FN_TCFACT_CAMB_Q02(TMDOCU_PROV.CO_MONE, @VSCO_MONE_NACI,TMDOCU_PROV.FE_EMIS, TMDOCU_PROV.IM_TOTA, @VSTI_CAMB, @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR,                          
  TMDOCU_PROV.FA_TIPO_CAMB))  'RETENCION' ,                       
   (CASE                          
  When  TMDOCU_PROV.CO_TIPO_DOCU IN (Select TI_DOCU_PROV From #TWTIPO_DOCU) THEN                          
  DBO.FU_FECH_PAGO( @ISCO_EMPR, TMDOCU_PROV.CO_PROV,  TMDOCU_PROV.CO_TIPO_DOCU, TMDOCU_PROV.NU_DOCU_PROV, TMDOCU_PROV.FE_VENC )                          
  Else                          
  NULL                          
  END) 'FE_VENC' , T3.NO_DOCU, 2, 'N', ''                  
 From  TMDOCU_PROV                        
 Left Outer join TTDOCU_CNTB T5                        
 On TMDOCU_PROV.TI_DOCU_ORIG = T5.TI_DOCU, TMAUXI_EMPR T2, TTDOCU_CNTB T3                          
 Where  TMDOCU_PROV.CO_EMPR  = @ISCO_EMPR                          
     And T2.CO_EMPR = TMDOCU_PROV.CO_EMPR                          
     And T2.TI_AUXI_EMPR = @VSCO_TIAU_PROV                          
     And T2.CO_AUXI_EMPR = TMDOCU_PROV.CO_PROV                          
     And T3.TI_DOCU = TMDOCU_PROV.CO_TIPO_DOCU                          
     And T3.ST_DOCU_0010 = 'S'                          
     And T3.ST_REGI_COMP = 'S'                          
--   And T5.TI_DOCU =* TMDOCU_PROV.TI_DOCU_ORIG                          
     And Exists ( Select #TWREGI_COMP_0002.NU_CORR_COMP                          
      From #TWREGI_COMP_0002                          
      Where                           
      #TWREGI_COMP_0002.CO_PROV = TMDOCU_PROV.CO_PROV And                          
      #TWREGI_COMP_0002.CO_TIPO_DOCU = TMDOCU_PROV.CO_TIPO_DOCU And              
      #TWREGI_COMP_0002.NU_DOCU_PROV = TMDOCU_PROV.NU_DOCU_PROV                          
     )                          
                    
            
                         
                          
 /*********** Incluir Registros Eliminados  **************/                          
 Select @VNNU_REGI_ELIM = Count(NU_CORR_COMP)                          
 From #TWREGI_COMP_0001                          
                          
 If IsNull(@VNNU_REGI_ELIM, 0 ) > 0 -- Existen Regitros Eliminado y seleciono la Opcion Incluir Reg Eliminados                          
  Insert Into #TWDOCU_PROV_FTEERAL                          
  (CO_TIPO, CO_SUNA, FE_EMIS, NU_CORR_COMP, NU_ASTO_REPO, NU_DOCU_PROV, CO_RUCC,NO_CORT_PROV,                           
  CO_TIPO_DOCU, CO_MONE,                          
  IM_BASE_GRA1,                    
  IM_BASE_GRA2,IM_BAIM_SCFI,IM_INAF_NACI,IM_IMSC_BAIM,IM_IMP1_DOG1,IM_IMP1_DOG2,IM_IMP1_SCFI,                    
  IM_IMPU_RENT,IM_IMPU_SOLI,IM_TOTA_NACI,            
  FA_TIPO_CAMB, FA_CAMB_EXTR,                           
  CO_IMP1, PC_IMP1,                          
  CO_IMP2, PC_IMP2,                          
  CO_IMP3, PC_IMP3,                          
  ST_SIGN, CO_ESTA_DOCU, CO_SUNA_ORIG, NU_DOCU_ORIG,                          
  FE_DOCU_ORIG, NU_DEPO_DETR, FE_DEPO_DETR, NU_COMP_SERV, FL_RETE, FE_ULTI_PAGO, VA_ORDE_0001,ST_FLAG_VISU, NU_FORM_NDCL)                          
  Select 'REG_COMPRAS', '', Null, SUBSTRING(NU_CORR_COMP, 1, 10), '', '', '', '**ANULADO**',                          
    '', '',                          
    0.00 , 0.00 , 0.00, 0.00 ,               
    0.00 ,                          
    0.00 , 0.00 , 0.00 ,                           
    0.00 , 0.00 ,                          
                 
 0.00 ,                           
    1.00, 1.00,                           
    '', 0.00,                          
    '', 0.00,                          
    '', 0.00,                          
    'S', 'ANU', '', '',                          
    Null, '', Null, '', Null, Null, 2, 'N', ''                          
  From #TWREGI_COMP_0001                          
  Where NOT EXISTS(  Select #TWDOCU_PROV_FTEERAL.NU_CORR_COMP                          
                             From #TWDOCU_PROV_FTEERAL                          
        Where #TWDOCU_PROV_FTEERAL.NU_CORR_COMP = #TWREGI_COMP_0001.NU_CORR_COMP)                        
            
end            
            
                    
-- ORDEN CASO OPCION OFICIAL                    
If @ISTI_REPO_0002 = 'O'                    
Begin                    
                    
If @ISTI_REPO = '1'                    
 Begin                     
 Select @VSOR_SELE = ' VA_ORDE_GENE, 4,VA_ORDE_0001,2 '                     
 End                    
             
If @ISTI_REPO = '2'                    
 Begin                     
 Select @VSOR_SELE = ' VA_ORDE_GENE, 4,VA_ORDE_0001,6,5 '                     
 End                    
                    
If @ISTI_REPO = '3'                    
 Begin                     
 Select @VSOR_SELE = ' VA_ORDE_GENE, 4,VA_ORDE_0001,3 '                     
 End                    
                    
If @ISTI_REPO = '4'                    
 Begin                     
 Select @VSOR_SELE = ' VA_ORDE_GENE, 6,VA_ORDE_0001,3 '                     
 End                    
                    
If @ISTI_REPO = '5'                    
 Begin                     
 Select @VSOR_SELE = ' VA_ORDE_GENE, 3 '                     
 End                    
             
If @ISTI_REPO = '6'                    
 Begin                     
 Select @VSOR_SELE = ' VA_ORDE_GENE, 25 '                     
 End                
                   
End                    
                    
                    
              
                    
--GRABA QUIEBRES                     
If @ISTI_REPO In ( '1','2','3')  And @ISTI_REPO_0002 = 'O'/*----- POR CODIGO SUNAT ------*/                    
 Begin                    
Insert Into #TWDOCU_PROV_FTEERAL                     
(CO_TIPO,FE_EMIS,NU_CORR_COMP,CO_SUNA,NU_DOCU_PROV,CO_RUCC,NO_CORT_PROV,IM_BASE_GRA1,                    
IM_BASE_GRA2,IM_BAIM_SCFI,IM_INAF_NACI,IM_IMSC_BAIM,IM_IMP1_DOG1,IM_IMP1_DOG2,IM_IMP1_SCFI,                    
IM_IMPU_RENT,IM_IMPU_SOLI,IM_TOTA_NACI,ST_SIGN,CO_ESTA_DOCU,NU_COMP_ORDE,CO_SUNA_ORIG,                    
NU_DOCU_ORIG,NO_DOCU,NU_ASTO_REPO,VA_ORDE_0001,ST_FLAG_VISU,CO_RUCC_0001,NO_CORT_PROV_0001)                    
Select                     
NULL,NULL,NULL,CO_SUNA,NULL,NULL,NULL,0,                    
0,0,0,0,0,0,0,                    
0,0,0,NULL,NULL,NULL,NULL,                    
NULL,Max(NO_DOCU),NULL,1,'S','DOCUMENTO   ' + CO_SUNA,Max(NO_DOCU)                    
From #TWDOCU_PROV_FTEERAL                    
Group By CO_SUNA                    
                    
                    
--GRABA TOTALES QUIEBRES(EN LOS TOTALES DEBE VALIDAR EL SIGNO DEL DOC Y EL ESTADO DEL MISMO)                     
Insert Into #TWDOCU_PROV_FTEERAL                     
(CO_TIPO,FE_EMIS,NU_CORR_COMP,CO_SUNA,NU_DOCU_PROV,CO_RUCC,NO_CORT_PROV,                    
IM_BASE_GRA1,IM_BASE_GRA2,IM_BAIM_SCFI,IM_INAF_NACI,IM_IMSC_BAIM,IM_IMP1_DOG1,IM_IMP1_DOG2,IM_IMP1_SCFI,                    
IM_IMPU_RENT,IM_IMPU_SOLI,IM_TOTA_NACI,                    
ST_SIGN,CO_ESTA_DOCU,NU_COMP_ORDE,CO_SUNA_ORIG,                    
NU_DOCU_ORIG,NO_DOCU,NU_ASTO_REPO,VA_ORDE_0001,ST_FLAG_VISU,CO_RUCC_0001,NO_CORT_PROV_0001,                    
IM_BASE_GRA1_PAR1,IM_BASE_GRA2_PAR1,IM_BAIM_SCFI_PAR1,IM_INAF_NACI_PAR1,IM_IMSC_BAIM_PAR1,IM_IMP1_DOG1_PAR1,IM_IMP1_DOG2_PAR1,                    
IM_IMP1_SCFI_PAR1,IM_IMPU_RENT_PAR1,IM_IMPU_SOLI_PAR1,IM_TOTA_NACI_PAR1                    
)                    
Select                     
NULL,NULL,NULL,CO_SUNA,NULL,NULL,NULL,                    
0,0,                    
0,0, 
0,0,                    
0,0,                    
0,0,                    
0,                    
NULL,NULL,NULL,NULL,                    
NULL,Max(NO_DOCU),NULL,3,'S','TOTAL ',Max(NO_DOCU),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_BASE_GRA1 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_BASE_GRA2 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_BAIM_SCFI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_INAF_NACI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMSC_BAIM End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMP1_DOG1 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),       
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMP1_DOG2 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMP1_SCFI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMPU_RENT End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMPU_SOLI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_TOTA_NACI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) )                    
From #TWDOCU_PROV_FTEERAL                    
Group By CO_SUNA                    
                    
-- Select @VSOR_SELE = ' VA_ORDE_GENE, 4,VA_ORDE_0001,3 '                     
                    
 End /*----- FIN POR CODIGO SUNAT ------*/                    
                    
--GRABA QUIEBRES                     
If @ISTI_REPO = '4'  And @ISTI_REPO_0002 = 'O'/*----- POR PROVEEDOR ------*/                   
 Begin                    
Insert Into #TWDOCU_PROV_FTEERAL                     
(CO_TIPO,FE_EMIS,NU_CORR_COMP,CO_SUNA,NU_DOCU_PROV,CO_RUCC,NO_CORT_PROV,IM_BASE_GRA1,                    
IM_BASE_GRA2,IM_BAIM_SCFI,IM_INAF_NACI,IM_IMSC_BAIM,IM_IMP1_DOG1,IM_IMP1_DOG2,IM_IMP1_SCFI,                    
IM_IMPU_RENT,IM_IMPU_SOLI,IM_TOTA_NACI,ST_SIGN,CO_ESTA_DOCU,NU_COMP_ORDE,CO_SUNA_ORIG,                    
NU_DOCU_ORIG,NO_DOCU,NU_ASTO_REPO,VA_ORDE_0001,ST_FLAG_VISU,CO_RUCC_0001,NO_CORT_PROV_0001)                    
Select                     
NULL,NULL,NULL,NULL,NULL,CO_RUCC,Max(NO_CORT_PROV),0,                    
0,0,0,0,0,0,0,                    
0,0,0,NULL,NULL,NULL,NULL,                    
NULL,NULL,NULL,1,'S','PROVEEDOR   ' + CO_RUCC,Max(NO_CORT_PROV)                    
From #TWDOCU_PROV_FTEERAL                    
Group By CO_RUCC                    
                    
                    
--GRABA TOTALES QUIEBRES(EN LOS TOTALES DEBE VALIDAR EL SIGNO DEL DOC Y EL ESTADO DEL MISMO)                     
Insert Into #TWDOCU_PROV_FTEERAL                     
(CO_TIPO,FE_EMIS,NU_CORR_COMP,CO_SUNA,NU_DOCU_PROV,CO_RUCC,NO_CORT_PROV,                    
IM_BASE_GRA1,IM_BASE_GRA2,IM_BAIM_SCFI,IM_INAF_NACI,IM_IMSC_BAIM,IM_IMP1_DOG1,IM_IMP1_DOG2,IM_IMP1_SCFI,                    
IM_IMPU_RENT,IM_IMPU_SOLI,IM_TOTA_NACI,                    
ST_SIGN,CO_ESTA_DOCU,NU_COMP_ORDE,CO_SUNA_ORIG,                    
NU_DOCU_ORIG,NO_DOCU,NU_ASTO_REPO,VA_ORDE_0001,ST_FLAG_VISU,CO_RUCC_0001,NO_CORT_PROV_0001,                    
IM_BASE_GRA1_PAR1,IM_BASE_GRA2_PAR1,IM_BAIM_SCFI_PAR1,IM_INAF_NACI_PAR1,IM_IMSC_BAIM_PAR1,IM_IMP1_DOG1_PAR1,IM_IMP1_DOG2_PAR1,                    
IM_IMP1_SCFI_PAR1,IM_IMPU_RENT_PAR1,IM_IMPU_SOLI_PAR1,IM_TOTA_NACI_PAR1                    
)                    
Select                     
NULL,NULL,NULL,NULL,NULL,CO_RUCC,Max(NO_CORT_PROV),                    
0,0,                    
0,0,                    
0,0,                    
0,0,                    
0,0,                    
0,                    
NULL,NULL,NULL,NULL,                    
NULL,NULL,NULL,3,'S','TOTAL ',Max(NO_CORT_PROV),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_BASE_GRA1 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_BASE_GRA2 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_BAIM_SCFI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_INAF_NACI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMSC_BAIM End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMP1_DOG1 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMP1_DOG2 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMP1_SCFI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMPU_RENT End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMPU_SOLI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_TOTA_NACI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) )                    
From #TWDOCU_PROV_FTEERAL                    
Group By CO_RUCC                    
                    
---Select @VSOR_SELE = ' VA_ORDE_GENE, 6,VA_ORDE_0001,3 '                     
                    
                    
 End /*----- FIN POR PROVEEDOR ------*/                    
                    
If @ISTI_REPO_0002 = 'O'                    
Begin                    
--GRABA TOTALES FINALES                    
Insert Into #TWDOCU_PROV_FTEERAL                     
(CO_TIPO,FE_EMIS,NU_CORR_COMP,CO_SUNA,NU_DOCU_PROV,CO_RUCC,NO_CORT_PROV,                    
IM_BASE_GRA1,IM_BASE_GRA2,IM_BAIM_SCFI,IM_INAF_NACI,IM_IMSC_BAIM,IM_IMP1_DOG1,IM_IMP1_DOG2,IM_IMP1_SCFI,                    
IM_IMPU_RENT,IM_IMPU_SOLI,IM_TOTA_NACI,                  
ST_SIGN,CO_ESTA_DOCU,NU_COMP_ORDE,CO_SUNA_ORIG,                    
NU_DOCU_ORIG,NO_DOCU,NU_ASTO_REPO,VA_ORDE_0001,ST_FLAG_VISU,CO_RUCC_0001,NO_CORT_PROV_0001,                    
IM_BASE_GRA1_PAR1,IM_BASE_GRA2_PAR1,IM_BAIM_SCFI_PAR1,IM_INAF_NACI_PAR1,IM_IMSC_BAIM_PAR1,IM_IMP1_DOG1_PAR1,IM_IMP1_DOG2_PAR1,                    
IM_IMP1_SCFI_PAR1,IM_IMPU_RENT_PAR1,IM_IMPU_SOLI_PAR1,IM_TOTA_NACI_PAR1,                                  
IM_BASE_GRA1_FIN1,IM_BASE_GRA2_FIN1,IM_BAIM_SCFI_FIN1,IM_INAF_NACI_FIN1,IM_IMSC_BAIM_FIN1,IM_IMP1_DOG1_FIN1,IM_IMP1_DOG2_FIN1,                    
IM_IMP1_SCFI_FIN1,IM_IMPU_RENT_FIN1,IM_IMPU_SOLI_FIN1,IM_TOTA_NACI_FIN1,                                  
VA_ORDE_GENE,ST_FLAG_VIFI,DE_FINA_0001                     
)                    
Select                     
NULL,NULL,NULL,NULL,NULL,NULL,NULL,                    
0,0,                    
0,0,                    
0,0,                    
0,0,                    
0,0,                    
0,                    
NULL,NULL,NULL,NULL,                    
NULL,NULL,NULL,4,'X',NULL,NULL,                    
0,0,0,0,0,0,0,0,0,0,0,                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_BASE_GRA1 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_BASE_GRA2 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_BAIM_SCFI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_INAF_NACI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMSC_BAIM End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMP1_DOG1 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMP1_DOG2 End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMP1_SCFI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMPU_RENT End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                   
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_IMPU_SOLI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
Sum(IsNull((Case When CO_ESTA_DOCU = 'ANU' Then 0 Else IM_TOTA_NACI End),0)*(Case When ST_SIGN = 'N' Then (-1) Else 1 End) ),                    
1,'S','TOTAL FINAL --->'                    
From #TWDOCU_PROV_FTEERAL                    
                    
Insert Into #TWDOCU_PROV_FTEERAL(DE_FINN_REPO,VA_ORDE_GENE,ST_FLFI_REPO)                    
Select '*** Fin del Reporte *** ',2,'S'                    
                    
End                    
                    
If @ISTI_REPO In ( '1','2','3','4', '6') And @ISTI_REPO_0002 = 'O'                    
Begin              
            
            
                  
Exec ('Select CO_TIPO,FE_EMIS,NU_CORR_COMP,CO_SUNA,NU_DOCU_PROV,CO_RUCC,NO_CORT_PROV,IM_BASE_GRA1,                    
IM_BASE_GRA2,IM_BAIM_SCFI,IM_INAF_NACI,IM_IMSC_BAIM,IM_IMP1_DOG1,IM_IMP1_DOG2,IM_IMP1_SCFI,                    
IM_IMPU_RENT,IM_IMPU_SOLI,IM_TOTA_NACI,ST_SIGN,CO_ESTA_DOCU,NU_COMP_ORDE,CO_SUNA_ORIG,                    
NU_DOCU_ORIG,NO_DOCU,NU_ASTO_REPO,                    
                    
ST_FLAG_VISU,ST_FLAG_VIFI,CO_RUCC_0001,NO_CORT_PROV_0001,                    
IM_BASE_GRA1_PAR1,IM_BASE_GRA2_PAR1,IM_BAIM_SCFI_PAR1,IM_INAF_NACI_PAR1,          
IM_IMSC_BAIM_PAR1,IM_IMP1_DOG1_PAR1,IM_IMP1_DOG2_PAR1,IM_IMP1_SCFI_PAR1,                    
IM_IMPU_RENT_PAR1,IM_IMPU_SOLI_PAR1,IM_TOTA_NACI_PAR1,                    
                    
IM_BASE_GRA1_FIN1,IM_BASE_GRA2_FIN1,IM_BAIM_SCFI_FIN1,IM_INAF_NACI_FIN1,IM_IMSC_BAIM_FIN1,IM_IMP1_DOG1_FIN1,IM_IMP1_DOG2_FIN1,                    
IM_IMP1_SCFI_FIN1,IM_IMPU_RENT_FIN1,IM_IMPU_SOLI_FIN1,IM_TOTA_NACI_FIN1,DE_FINA_0001,                    
DE_FINN_REPO,ST_FLFI_REPO, FE_ULTI_PAGO, NU_DEPO_DETR, FE_DEPO_DETR, FL_RETE, NU_FORM_NDCL, NU_ASTO      
From #TWDOCU_PROV_FTEERAL                    
Order By ' + @VSOR_SELE)                    
End                    
                    
If @ISTI_REPO = '5' And @ISTI_REPO_0002 = 'O'                     
Begin               
             
               
                
Exec ('Select CO_TIPO,FE_EMIS,NU_CORR_COMP,CO_SUNA,NU_DOCU_PROV,CO_RUCC,NO_CORT_PROV,IM_BASE_GRA1,                    
IM_BASE_GRA2,IM_BAIM_SCFI,IM_INAF_NACI,IM_IMSC_BAIM,IM_IMP1_DOG1,IM_IMP1_DOG2,IM_IMP1_SCFI,                    
IM_IMPU_RENT,IM_IMPU_SOLI,IM_TOTA_NACI,ST_SIGN,CO_ESTA_DOCU,NU_COMP_ORDE,CO_SUNA_ORIG,                    
NU_DOCU_ORIG,NO_DOCU,NU_ASTO_REPO,                    
ST_FLAG_VISU,ST_FLAG_VIFI,                    
IM_BASE_GRA1_FIN1,IM_BASE_GRA2_FIN1,IM_BAIM_SCFI_FIN1,IM_INAF_NACI_FIN1,IM_IMSC_BAIM_FIN1,IM_IMP1_DOG1_FIN1,IM_IMP1_DOG2_FIN1,                    
IM_IMP1_SCFI_FIN1,IM_IMPU_RENT_FIN1,IM_IMPU_SOLI_FIN1,IM_TOTA_NACI_FIN1,DE_FINA_0001,                    
DE_FINN_REPO,ST_FLFI_REPO, FE_ULTI_PAGO, NU_DEPO_DETR, FE_DEPO_DETR, FL_RETE, NU_FORM_NDCL, NU_ASTO                  
From #TWDOCU_PROV_FTEERAL                    
Order By ' + @VSOR_SELE)                    
End                    
                    
                    
                    
Return                              
                             
/*--------------------------------------------- Fin ---------------------------------------------------------------*/


GO
